package net.dgg.zz.common.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.service.ArService;
import net.dgg.iboss.base.util.StringUtils;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.common.service.CommonMangerExtService;
import net.dgg.zz.common.service.ZzFilesRecordService;
import net.dgg.zz.production.suspend.service.ZzSubOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 赵有文
 * @despriction 生产通用弹出层controller
 * @create 2018/5/25 0025 上午 11:01
 **/
@Controller
@RequestMapping("/zzsc/commonLayer")
public class ZzCommonLayerController extends DggBaseController {
    @Autowired
    private CommonMangerExtService commonMangerExtService;
    @Autowired
    private ArService arService;
    @Autowired
    private ZzFilesRecordService scFilesRecordService;
    @Autowired
    private ZzSubOrderService scSubOrderService;
    @Autowired
    private SessionManager sessionManager;


    /**
     * 弹出选择商品界面
     *
     * @return
     */
    @RequestMapping("/selectProduct")
    public String toSelectProduct() {

        return "/sc/commonLayer/layer_select_product";
    }

    /**
     * 弹出选择用户界面
     *
     * @return
     */
    @RequestMapping("/selectUser")
    public String toSelectUser() {

        return "/sc/commonLayer/layer_select_user";
    }

    /**
     * 弹出绑定档案界面
     *
     * @return
     */
    // @AuthOpt(code = "SC_GENERAL_BIND_FILES")
    @RequestMapping("/layer_bind_files")
    public String toBindFiles(String id, String no, ModelMap modelMap) {

        List<Map> recList = scFilesRecordService.query(id);
        int size = recList.size();
        String recFilesNos = "", recFilesIds = "";
        String[] recFilesNoArr = new String[size];
        String[] recFilesIdArr = new String[size];
        if (size > 0) {

            for (int i = 0; i < size; i++) {
                recFilesNoArr[i] = recList.get(i).get("filesNo") + "";
                recFilesIdArr[i] = recList.get(i).get("filesId") + "";
            }
            recFilesNos = StringUtils.join(recFilesNoArr, " , ");
            recFilesIds = StringUtils.join(recFilesIdArr, ",");
        } else {
            recFilesNos = "暂未关联";
        }
        modelMap.addAttribute("orderId", id);
        modelMap.addAttribute("orderNo", no);
        modelMap.addAttribute("recFilesNos", recFilesNos);
        modelMap.addAttribute("recFilesIds", recFilesIds);
        return "/zzsc/commonLayer/layer_bind_files";
    }


    /**
     * 查询用户数据
     *
     * @return
     */
    @RequestMapping("/queryUser")
    @ResponseBody
    public DataTableResponse queryUser(@RequestParam Map paras, HttpServletRequest request) {

        DataTableRequest r = this.generatePatameter(paras, request);
        int start = Integer.valueOf(paras.get("start") + "");
        int limit = Integer.valueOf(paras.get("limit") + "");
        int page = start / limit + 1;

        paras.put("page", page);
        paras.put("locked", "0");
        List<Map> list = commonMangerExtService.searchUserPage(paras);
        int total = Integer.parseInt(paras.get("count") + "");
        return new DataTableResponse(r.getDraw(), total, total, list, "");
    }


    /**
     * 查询档案数据
     *
     * @return
     */
    @RequestMapping("/queryFiles")
    @ResponseBody
    public DataTableResponse queryFiles(@RequestParam Map paras, HttpServletRequest request) {

        DataTableRequest r = this.generatePatameter(paras, request);
        int count = 0;
        List<Map> filesList = new ArrayList<>();
        try {
            filesList = scFilesRecordService.queryFiles(paras);
            count = (int) paras.get("totalCount");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return new DataTableResponse(r.getDraw(), count, count, filesList, "");

    }

    @RequestMapping("/bindOrUnbindFiles")
    @ResponseBody
    public DggRestResponse bindOrUnbindFiles(@RequestParam Map paras) {
        UserEntity user = sessionManager.getCurrentUser();
        String operate = paras.get("operate") + "";
        if (operate.equals("绑定")) {
            Map order = scSubOrderService.queryMapById(paras.get("orderId") + "");
            List<Map> filesList = new ArrayList<>();
            filesList.add(paras);

            scFilesRecordService.insert(user, order, filesList);
        } else if (operate.equals("解除绑定")) {

            scFilesRecordService.invalid(paras);
        }
        return this.getSuccessResponse("");
    }

    @RequestMapping("/verifyOrderExist")
    @ResponseBody
    public DggRestResponse verifyOrderExist(String orderNo) {
        try {
            Map order = scSubOrderService.queryMapByNo(orderNo);
            return this.getSuccessResponse(order);
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }

    }

    /**
     * 生成sql查询的分页、排序参数 limit start sortCol sortType
     *
     * @param paras
     * @param request
     * @return
     */
    protected DataTableRequest generatePatameter(Map paras, HttpServletRequest request) {
        DataTableRequest r = DataTableUtils.getParam(request);
        //分页参数
        paras.put("start", (r.getPage() - 1) * r.getLength());
        paras.put("limit", r.getLength());
        //排序列 排序方式
        String orderCol = null, sortType = null;

        if (null != r.getOrder() && r.getOrder().size() > 0) {
            int orderColIndex = r.getOrder().get(0).getColumn();
            orderCol = r.getColumns().get(orderColIndex).getData();
            sortType = r.getOrder().get(0).getDir();
        } else {
            orderCol = "createTime";
            sortType = "desc";
        }
        paras.put("sortCol", orderCol);
        paras.put("sortType", sortType);

        return r;
    }
}
